package com.abocode.aichat.server.context;

import com.abocode.aichat.data.ServerContextName;
import com.abocode.aichat.exception.ServerContextException;

import java.util.HashMap;
import java.util.Map;

/**
 * @author guanxf
 */
public abstract class AbstractServerContext<T extends ServerContextName, K extends ServerContextName> implements ServerContext<T, K> {

    private Map<String, T> context = new HashMap<>();

    @Override
    public T get(K k) {
        if (isContains(k.name())) {
            return context.get(k.name());
        }
        throw new ServerContextException(k.name() + " not register");
    }

    @Override
    public void register(T t) {
        if (!isContains(t.name())) {
            context.put(t.name(), t);
            return;
        }
        throw new ServerContextException(t.getClass().getName() + "register");
    }

    public boolean isContains(String key) {
       return context.containsKey(key);
    }
}
